package com.itcoon.transform4j.demo.web.controller;

import com.itcoon.common.web.core.response.ApiResponse;
import com.itcoon.transform4j.demo.web.vo.*;
import com.itcoon.transform.starter.Transformer;
import com.itcoon.transform4j.demo.domain.Company;
import com.itcoon.transform4j.demo.domain.Student;
import com.itcoon.transform4j.demo.domain.Teacher;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "10_inject_基础篇", description = "inject 测试")
@RestController
@RequestMapping("/api")
public class BaseInjectController {

    @ApiOperation("获取教师信息-simple， 目标为接口定义")
    @GetMapping("/teacher")
    public ApiResponse<TeacherVIO> getTeacher(){
        Teacher teacher = new Teacher();
        teacher.setId(1L);
        teacher.setName("王老师");

        Student student1 = new Student();
        student1.setSid(1L);
        student1.setName("李同学");

        Student student2 = new Student();
        student2.setSid(2L);
        student2.setName("岚同学");

        teacher.setStudent(student1);


        TeacherVIO result = Transformer
                .to(TeacherVIO.class)
                .apply(teacher)
                .setAt("name", "变异王老师")
                .setAt("student", ()-> Transformer.to(StudentVIO.class).apply(student2).done())
                .done();

        return ApiResponse.createSuccess(result);
    }

    @ApiOperation("获取教师信息-目标为接口定义，级联属性注入")
    @GetMapping("/teacher2")
    public ApiResponse<TeacherVIO> getTeacher2(){
        Teacher teacher = new Teacher();
        teacher.setId(1L);
        teacher.setName("王老师");

        Student student1 = new Student();
        student1.setSid(1L);
        student1.setName("李同学");

        teacher.setStudent(student1);

        TeacherVIO result = Transformer
                .to(TeacherVIO.class)
                .apply(teacher)
                .setAt("name", "变异王老师")
                .setAt("student.name", "黄同学")
                .setAt("student.age", 20)
                .done();

        return ApiResponse.createSuccess(result);
    }

    @ApiOperation("获取教师信息-simple， 目标为类定义")
    @GetMapping("/teacher3")
    public ApiResponse<TeacherVCO> getTeacher3(){
        Teacher teacher = new Teacher();
        teacher.setId(1L);
        teacher.setName("王老师");

        Student student1 = new Student();
        student1.setSid(1L);
        student1.setName("李同学");

        Student student2 = new Student();
        student2.setSid(2L);
        student2.setName("岚同学");

        teacher.setStudent(student1);


        TeacherVCO result = Transformer
                .to(TeacherVCO.class)
                .apply(teacher)
                .setAt("name", "变异王老师")
                .setAt("student", ()-> Transformer.to(StudentVCO.class).apply(student2).done())
                .done();

        return ApiResponse.createSuccess(result);
    }

    @ApiOperation("获取教师信息-目标为类定义，级联属性注入")
    @GetMapping("/teacher4")
    public ApiResponse<TeacherVCO> getTeacher4(){
        Teacher teacher = new Teacher();
        teacher.setId(1L);
        teacher.setName("王老师");

        Student student1 = new Student();
        student1.setSid(1L);
        student1.setName("李同学");

        teacher.setStudent(student1);

        TeacherVCO result = Transformer
                .to(TeacherVCO.class)
                .apply(teacher)
                .setAt("name", name->"变异" + name)
//                .setAt("age", 22)
                .setAt("student.name", "黄同学")
                .setAt("student.age", 20)
                .done();

        return ApiResponse.createSuccess(result);
    }


    @ApiOperation("公司企业——插入源对象不存在的属性")
    @GetMapping("/company")
    public ApiResponse<CompanyVCO> getCompany(){
        Company company = new Company();
        company.setName("小马企业");
        company.setLocation("华天大夏501");
        CompanyVCO companyVCO = Transformer.to(CompanyVCO.class).apply(company).setAt("workers", 54).done();
        return ApiResponse.createSuccess(companyVCO);
    }
}
